﻿#include "widget.h"
#include "ui_widget.h"
#include "widget2.h"
#include "widget3.h"
#include "widget4.h"
#include "WordEngine.h"
#include "common.h"
#include "QWaiting.h"
#include "worker.h"
#include <QMenu>
#include <QMouseEvent>
#include <QMessageBox>
#include <QScreen>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    // 获取主屏幕的大小
    QScreen *screen = QGuiApplication::primaryScreen();
    QSize screenSize = screen->availableGeometry().size();
    this->setFixedSize(screenSize);
    //界面2
    m_pWidget2 = new Widget2;
    connect(m_pWidget2,SIGNAL(signalNextPage()),this,SLOT(SlotWidget2NextPage()));
    connect(m_pWidget2,SIGNAL(signalLastPage()),this,SLOT(SlotWidget2LastPage()));
    ui->stackedWidget->addWidget(m_pWidget2);

    //界面3
    m_pWidget3 = new Widget3;
    connect(m_pWidget3,SIGNAL(SignalWidget3Chang(int)),this,SLOT(SlotWidget3Change(int)));
    ui->stackedWidget->addWidget(m_pWidget3);

    //界面4
    m_pWidget4 = new Widget4;
    connect(m_pWidget4,SIGNAL(SignalWidget4Next(QPair<QString,QString>)),this,SLOT(SlotWidget4Next(QPair<QString,QString>)));
    ui->stackedWidget->addWidget(m_pWidget4);

    //界面5
    m_pWidget5 = new Widget5;
    connect(m_pWidget5,&Widget5::SignalWidget5Back,[=](){
       ui->stackedWidget->setCurrentIndex(0);
    });
    ui->stackedWidget->addWidget(m_pWidget5);

    m_pQwait = new QWaiting(this);

    qRegisterMetaType<QPair<QString,QString>>("QPair<QString,QString>");
    Worker *worker = new Worker;
    worker->moveToThread(&workerThread);
    connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
    connect(this, &Widget::operate, worker, &Worker::doWork);
    connect(m_pWidget5,&Widget5::SignalWidget5Print,worker,&Worker::doWork2);
    connect(worker, &Worker::resultReady, this, &Widget::handleResults);
    workerThread.start();

    setWindowFlags(Qt::FramelessWindowHint);
    showFullScreen();
}

Widget::~Widget()
{
    delete ui;
    workerThread.quit();
    workerThread.wait();
}

void Widget::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        if(ui->stackedWidget->currentIndex() == 0)
        {
            m_pWidget2->clearWidgetData();
            ui->stackedWidget->setCurrentIndex(1);
        }
    }
}

void Widget::contextMenuEvent(QContextMenuEvent *event)
{
    //if(ui->stackedWidget->currentIndex() == 0)
    {
        QMenu menu(this);
        QAction *closeAction = menu.addAction(QStringLiteral("关闭"));
        connect(closeAction, &QAction::triggered, qApp, &QApplication::quit);
        menu.exec(event->globalPos());
    }
}

void Widget::SlotWidget2NextPage()
{
    ui->stackedWidget->setCurrentIndex(2);
}

void Widget::SlotWidget2LastPage()
{
    ui->stackedWidget->setCurrentIndex(0);
}

void Widget::SlotWidget3Change(int index)
{
    m_pWidget4->setShowData(index);
    ui->stackedWidget->setCurrentIndex(3);
}

void Widget::SlotWidget4Next(QPair<QString, QString> recode)
{
    QStringList _list = m_pWidget2->getwidgetData();
    m_pWidget5->clearWidget();
    ui->stackedWidget->setCurrentIndex(4);
    m_pQwait->show();
    QApplication::processEvents();
    emit operate(recode,_list);
}

void Widget::handleResults(const QString &)
{
    m_pQwait->close();
    m_pWidget5->loadWord();
}
